import { _decorator, assetManager, AssetManager, Component, EventTarget, JsonAsset, Node, resources } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('GameData')
export class GameData {
    private static instance: GameData;
    public static get Instance(): GameData {
        if (!this.instance) {
            this.instance = new GameData;
            this.instance.Init();
        }
        return this.instance;
    }

    static gameName: string;
    static tutorial = 0;
    static defaultData: Record<string, any>;
    static prefabsBundle: AssetManager.Bundle;
    static clothesBundle: AssetManager.Bundle;
    static itemBundle: AssetManager.Bundle;
    static eventTarget = new EventTarget;
    private static dataLoad = 0;
    public static set DataLoad(v: number) {
        this.dataLoad = v;
        if (this.dataLoad == 3) {
            this.eventTarget.emit("DataLoad");
        }
    }
    public static get DataLoad() {
        return this.dataLoad;
    }


    private Init() {
        // localStorage.clear();
        let array = ["Tutorial"];
        for (let i = 0; i < array.length; i++) {
            const element = array[i];
            if (localStorage.getItem("Miga_" + element) == null || localStorage.getItem("Miga_" + element) == "") localStorage.setItem("Miga_" + element, "0");
        }
        let array2 = [];
        for (let i = 0; i < array2.length; i++) {
            const element = array2[i];
            if (localStorage.getItem("Miga_" + element) == null || localStorage.getItem("Miga_" + element) == "") localStorage.setItem("Miga_" + element, JSON.stringify([]));
        }
        resources.load("Data/GameData", JsonAsset, (err, data) => {
            if (err) return console.error(err.message);
            GameData.defaultData = data.json;
            let array3 = ["PersonData"];
            for (let i = 0; i < array3.length; i++) {
                const element = array3[i];
                if (localStorage.getItem("Miga_" + element) == null || localStorage.getItem("Miga_" + element) == "") localStorage.setItem("Miga_" + element, JSON.stringify(element == "PersonData" ? GameData.defaultData.PersonData : {}));
            }
        });
    }

    public getNumberData(str: string): number {
        let data = localStorage.getItem("Miga_" + str);
        return parseInt(data);
    }

    public setNumberData(str: string, value: number) {
        localStorage.setItem("Miga_" + str, value.toString());
    }

    public getArrayData<T>(str: string): T[] {
        let data = localStorage.getItem("Miga_" + str);
        return JSON.parse(data);
    }

    public setArrayData<T>(str: string, value: T[]) {
        localStorage.setItem("Miga_" + str, JSON.stringify(value));
    }

    public getRecordData(str: string) {
        let data = localStorage.getItem("Miga_" + str);
        return JSON.parse(data);
    }

    public setRecordData(str: string, value: any) {
        localStorage.setItem("Miga_" + str, JSON.stringify(value));
    }
}

export enum WearType {
    "headwear",
    "up",
    "bottom",
    "belt",
    "cloak",
    "Lhandwear",
    "Rhandwear",
    "necklace",
    "scarf",
    "shoe"
}

export enum CharacterWearType {
    "hair",
    "headwear",
    "up",
    "bottom",
    "eye",
    "mouth",
    "eyebrow",
    "belt",
    "cloak",
    "Learwear",
    "Rearwear",
    "Lhandwear",
    "Rhandwear",
    "necklace",
    "scarf",
    "shoe"
}


